package com.nuoniu.sibanyun.controller.erp.in;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.common.web.http.UserAgentUtils;
import com.nuoniu.sibanyun.entity.erp.in.InInventory;
import com.nuoniu.sibanyun.entity.erp.in.InUnit;
import com.nuoniu.sibanyun.entity.erp.in.InUnitGroup;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.in.IInInventoryService;
import com.nuoniu.sibanyun.service.erp.in.IInUnitGroupService;
import com.nuoniu.sibanyun.service.erp.in.IInUnitService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 计量单位 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-25
 */
@RestController
@RequestMapping("/in/unit")
@Api(value = "计量单位管理", tags = "计量单位管理")
public class InUnitController extends BaseController {

    @Autowired
    private IInUnitService unitService;
    @Autowired
    private IInUnitGroupService unitGroupService;
    @Autowired
    private IInInventoryService inventoryService;
    @Autowired
    private IErpAccountService accountService;

    /**
     * 分页获取计量单位
     * @author 小啊刚
     * @date 2020-09-25
     */
    @GetMapping("pageList")
    @ApiOperation(value = "分页获取计量单位")
    public HttpResult pageList(Page page, InUnit unit) {
        unit.setAccountId(accountService.getCurrentAccountId());
        Page<InUnit> unitPage = unitService.pageList(page, unit);
        return HttpResult.ok(unitPage);
    }

    @GetMapping("getAll")
    @ApiOperation(value = "获取所有计量单位")
    public HttpResult getAll() {
        InUnit unit = new InUnit();
        QueryWrapper<InUnit> queryWrapper = new QueryWrapper<>(unit);
        queryWrapper.eq("company_id", UserThreadLocal.get().getCompanyId());
        queryWrapper.eq("account_id", accountService.getCurrentAccountId());
        return HttpResult.ok(unitService.list(queryWrapper));
    }

    /**
     * 保存多计量单位
     * @author 小啊刚
     * @date 2020-09-25
     */
    @PostMapping("save")
    @ApiOperation(value = "保存多计量单位")
    public HttpResult save(@RequestBody List<InUnit> units) {
        Map<String, String> nameMap = new HashMap<>();

        if(units.size() < 1) {
            throw new NuoNiuException("至少一个主计量和辅计量");
        }

        if(StringUtils.isBlank(units.get(0).getUnitGroupName())) {
            throw new NuoNiuException("计量单位组名称不能为空");
        }

        if(StringUtils.isBlank(units.get(0).getCode())) {
            throw new NuoNiuException("计量单位组编码不能为空");
        }

        for(InUnit unit : units) {
            if(StringUtils.isBlank(unit.getName())) {
                throw new NuoNiuException("计量单位名称不能为空");
            }

            if(nameMap.get(unit.getName()) != null) {
                throw new NuoNiuException("计量单位名称重复: " + unit.getName());
            }

            nameMap.put(unit.getName(), unit.getName());
        }

        InUnitGroup unitGroup = unitGroupService.getByName(units.get(0).getUnitGroupName());
        if(unitGroup != null) {
            if(units.get(0).getUnitGroupId() == null || !units.get(0).getUnitGroupId().equals(unitGroup.getId().intValue())) {
                throw new NuoNiuException("计量单位组名称已存在");
            }
        }

        InUnitGroup groupByCode = unitGroupService.getByCode(units.get(0).getCode());
        if(groupByCode != null) {
            if(units.get(0).getUnitGroupId() == null || !units.get(0).getUnitGroupId().equals(groupByCode.getId().intValue())) {
                throw new NuoNiuException("计量单位组编码已存在");
            }
        }

        return HttpResult.ok(unitService.saveUnitList(units));
    }

    /**
     * 保存单计量单位
     * @author 小啊刚
     * @date 2020-09-25
     */
    @PostMapping("saveUnit")
    @ApiOperation(value = "保存单计量单位")
    public HttpResult saveUnit(@RequestBody InUnit unit) {
        if(StringUtils.isBlank(unit.getName())) {
            throw new NuoNiuException("计量名称不能为空");
        }

        if(StringUtils.isBlank(unit.getCode())) {
            throw new NuoNiuException("计量编码不能为空");
        }

        InUnit unitByName = unitService.getByName(unit.getName());
        if(unitByName != null) {
            if(unit.getId() == null || !unit.getId().equals(unitByName.getId())) {
                throw new NuoNiuException("计量名称已存在");
            }
        }

        InUnit unitByCode = unitService.getByCode(unit.getCode());
        if(unitByCode != null) {
            if(unit.getId() == null || !unit.getId().equals(unitByCode.getId())) {
                throw new NuoNiuException("计量编码已存在");
            }
        }

        unitService.saveUnit(unit);
        return HttpResult.ok();
    }

    /**
     * 删除计量单位
     * @author 小啊刚
     * @date 2020-09-25
     */
    @PostMapping("delete")
    @ApiOperation(value = "删除计量单位")
    public HttpResult delete(@RequestBody List<Integer> ids) {
        if(ids.size() > 0) {
            Integer id = ids.get(0);
            InUnit unit = unitService.getById(id);
            if(unit != null) {
                if(unit.getUnitGroupId() == 0) {
                    List<InInventory> inventoryList = inventoryService.getByUnitId(id);
                    if(inventoryList.size() > 0) {
                        throw new NuoNiuException("已在物料中使用,不能删除");
                    }
                    unitService.removeById(id);
                }else {
                    List<InInventory> inventoryList = inventoryService.getByUnitGroupId(unit.getUnitGroupId());
                    if(inventoryList.size() > 0) {
                        throw new NuoNiuException("已在物料中使用,不能删除");
                    }
                    unitService.getRemoveByGroupId(unit.getUnitGroupId());
                    unitGroupService.removeById(unit.getUnitGroupId());
                }
            }
        }
        return HttpResult.ok();
    }





}


